#include "dd_animation.h"

/* initialize animation to defaults
 * counter is at 0, animation is moving negatively (so it doesn't move)
 * and max is given
 */
void dd_animation_init(struct dd_animation *a, anim_size max_value) {
	a->counter = 0;
	a->max = max_value;
	a->positive = 0;
}

/* update animation, return true if something changed, false otherwise */
int dd_animation_update(struct dd_animation *a) {
	/* Positive animation */
	if (a->positive)
	{
		/* Counter is below max */
		if (a->counter < a->max)
		{
			/* Advance animation and return updated */
			a->counter++;
			return 1;
		}
	}
	/* Negative animation */
	else
	{
		/* Counter is above zero */
		if (a->counter > 0)
		{
			/* Advance animation (negatively) and return updated */
			a->counter--;
			return 1;
		}
	}

	/* Animation is not updated */
	return 0;
}

/* start animation from 0 */
void dd_animation_start0(struct dd_animation *a) {
	a->counter = 0;
	a->positive = 1;
}

/* start animation from max */
void dd_animation_startmax(struct dd_animation *a) {
	a->counter = a->max;
	a->positive = 0;
}

/* flip animation */
void dd_animation_flip(struct dd_animation *a) {
	a->positive ^= 1;
}

/* interpolate */
float dd_animation_interpolate(struct dd_animation *a, float value) {
	return value *a->counter /a->max;
}

/* issers */
int dd_animation_isrunning(struct dd_animation *a) {
	/* if counter is bellow max and moves positively OR
		  counter is above  0   and moves negatively
		  animation is running
	 */
	return (a->counter < a->max &&  a->positive)
		|| (a->counter > 0		&& !a->positive);
}
